package com.pdd.controller;

import com.pdd.beans.User;
import com.pdd.dto.DTO;
import com.pdd.service.UserService;
import com.pdd.util.EncryptUtil;
import com.pdd.util.UploadUtil;
import com.pdd.vo.user.UserAddVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户控制器
 */
@Api(description = "用户接口",tags = {"用户接口"})
@CrossOrigin(origins = { "*" })
@Controller
public class UserController {

    @Autowired
    @Setter
    private UserService userService;

    @ApiOperation(value = "添加用户信息")
    @RequestMapping(value = "/user", method = RequestMethod.POST)
    @ResponseBody
    public DTO addUser(MultipartFile photo, UserAddVO vo, HttpServletRequest request, EncryptUtil md5){
        // 1. 执行上传，获取上传后的文件名
        String targetName = UploadUtil.upload(photo, request);
        // 1. 封装参数
        User user = new User();
        user.setEmail(vo.getEmail());
        user.setPwd(md5.getMD5(vo.getPwd()));
        user.setName(vo.getName());
        user.setPhoto(targetName);
        user.setScore(vo.getScore());
        // 2. 执行新增
        boolean result = userService.addUser(user);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }
    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.DELETE)
    public DTO delUser(@PathVariable(value = "id") Integer id){

        boolean result = userService.delUser(id);
        if (result){
            return DTO.success();
        }else {
            return DTO.error();
        }
    }

    @ApiOperation(value = "更新用户信息")
    @RequestMapping(value = "/user", method = RequestMethod.PUT)
    @ResponseBody
    public DTO uplUser(MultipartFile photo,UserAddVO vo,HttpServletRequest request,EncryptUtil md5){
        // 1. 执行上传，获取上传后的文件名
        String targetName = UploadUtil.upload(photo, request);
        // 1. 封装参数
        User user = new User();
        user.setEmail(vo.getEmail());
        user.setPwd(md5.getMD5(vo.getPwd()));
        user.setName(vo.getName());
        user.setPhoto(targetName);
        user.setScore(vo.getScore());

        // 2. 执行更新
        boolean result = userService.uplUser(user);

        // 3. 创建DTO，并返回
        if (result){
            return DTO.success();
        }else{
            return DTO.error();
        }
    }

    @ApiOperation(value = "查询用户信息")
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @ResponseBody
    public DTO getUser(){
        // 1. 执行查询操作
        List<User> list = userService.getUser();

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
    @ApiOperation(value = "根据ID查询用户信息")
    @RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
    @ResponseBody
    public DTO getById(@PathVariable(value = "id") Integer id){
        // 1. 执行查询操作
        User list = userService.getById(id);

        // 2. 封装 DTO
        DTO dto = new DTO(list);

        // 3. 返回结果
        return dto;
    }
}
